package com.qd.panda.carpark;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.carpark.*;
import com.qd.panda.service.park.CarParkChargeTimeService;
import com.qd.panda.service.park.CarParkService;
import com.qd.panda.service.park.ParkStrokeHolderService;
import com.qd.panda.service.stroke.UserStrokeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * APP_停车场信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/carPark/")
@Slf4j
public class AppCarParkController {
    private final CarParkService carParkService;
    private final CarParkChargeTimeService carParkChargeTimeService;
    private final ParkStrokeHolderService parkStrokeHolderService;
    private final UserStrokeService userStrokeService;

    /**
     * 停车场信息列表
     *
     * @param searchCarParkDTO lat   纬度 lng   经度
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<CarParkInfoDTO> list(SearchCarParkDTO searchCarParkDTO) {
        Double lat = searchCarParkDTO.getLat();
        Double lng = searchCarParkDTO.getLng();
        Long count = searchCarParkDTO.getPageSize();
        if (null == lat || lat < 1) {
            throw new ApiException("纬度值错误");
        }
        if (null == lng || lng < 1) {
            throw new ApiException("经度值错误");
        }
        if (null == count || count < 1) {
            count = 10L;
        }
        if (count > 1000) {
            throw new ApiException("超过count支持的最大值");
        }
        return carParkService.getCarParkInfoList(lat, lng, count);
    }

    /**
     * 查询停车场信息
     *
     * @param searchCarParkDTO lat   纬度 lng   经度
     * @return 列表信息
     */
    @GetMapping(value = "/search")
    public IPage<CarParkInfoDTO> search(SearchCarParkDTO searchCarParkDTO) {
        return carParkService.search(PageUtil.buildPage(searchCarParkDTO), searchCarParkDTO);
    }

    /**
     * 获取停车场信息
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public PersonCarPark getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return parkStrokeHolderService.getInfoById(id);
    }

    /**
     * 获得管辖停车场
     * 保安使用
     *
     * @return 列表信息
     */
    @GetMapping(value = "/jurisdictionCarPark")
    public List<CarParkDTO> jurisdictionCarPark() {
        return carParkService.jurisdictionCarPark();
    }

    /**
     * 门岗首页数量统计
     *
     * @return 数量
     */
    @GetMapping(value = "/doorIndexCount")
    public List<Map<String, Long>> doorIndexCount(@Validated @NotBlank(message = "停车场ID不能为空") String parkId) {
        return userStrokeService.getCount(parkId);
    }

    /**
     * 开启关闭
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/openOrClose")
    public ApiResultWrapper<Object> openOrClose(@Validated({DefaultGroup.class}) @RequestBody CarParkLogDTO dto) {
        carParkService.openOrClose(dto);
        return ApiResult.success();
    }

    /**
     * 充电站时段
     *
     * @param parkId 停车场ID
     * @return 页面对象
     */
    @GetMapping(value = "/getParkChargeTime")
    public List<CarParkChargeTimeDTO> getParkChargeTime(@Validated @NotBlank(message = "停车场ID不能为空") String parkId) {
        return carParkChargeTimeService.getListByPId(parkId);
    }
}